package com.harry.home.wordpicture.Fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.harry.home.wordpicture.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private List<View> list;
    private ViewPager vp;
    private RadioGroup rg;
    private int initIndex = 0;
    private int[] imageId = new int[]{R.mipmap.splash,R.drawable.icon,R.drawable.umeng_xp_seek};
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home,container,false);
        vp = (ViewPager) v.findViewById(R.id.vp);
        vp.setAdapter(new MyPagerAdapter());
      /*  rg = (RadioGroup) v.findViewById(R.id.rg1);
        ((RadioButton)rg.getChildAt(initIndex)).setChecked(true);
        vp.setCurrentItem(initIndex);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                int index = group.indexOfChild(rb);
                vp.setCurrentItem(index);
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) rg.getChildAt(position);
                rb.setChecked(true);
            }
            public void onPageScrollStateChanged(int state) {
            }
        });*/
        return v;
    }
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rg = (RadioGroup) view.findViewById(R.id.rg1);
        ((RadioButton)rg.getChildAt(initIndex)).setChecked(true);
        vp.setCurrentItem(initIndex);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                int index = group.indexOfChild(rb);
                vp.setCurrentItem(index);
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) rg.getChildAt(position);
                rb.setChecked(true);
            }
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class MyPagerAdapter extends PagerAdapter{

        public int getCount() {
            return list.size();
        }
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v = list.get(position);
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView(list.get(position));
        }
    }
    public void initData(){
        list = new ArrayList<View>();
        for (int i=0; i < imageId.length; i++){
            ImageView iv = new ImageView(getActivity());
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setImageResource(imageId[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            list.add(iv);
        }
    }
}
